package collector;

import parser.CodeReviewToken;
import standardize.FragmentFormatter;
import standardize.NaturalForm;

import java.io.IOException;

/**
 * @author dotekien
 *         Date: Mar 13, 2006
 *         Time: 5:32:12 PM
 */
public class Chunk {
    private final FileObj file;   //file holding the chunk
    private int startIndexOnMatrix; //startIndexOnMatrix and chunk length is a sign to identify a chunk
    private Chain chain;    //store chunk length in a chain holding this chunk

    //this piece information is for to display chunk on gui or report
    //reflect the real file
    private int lineStart;
    private int lineEnd;
    private int indexStart;
    private int indexEnd;

    //for display chunk in nice format
    private int sIndex;
    private int eIndex;

    public Chunk(FileObj file, Statement first, Statement last, int startPointOnMatrix) {
        this.file = file;
        //debug only
        //System.out.println(first.toString());
        //System.out.println(last.toString()+ "\n");

        NaturalForm form = new NaturalForm(file.getTokens(), file.getTokens().get(first.getsTokenIndex()));
        CodeReviewToken firstT = form.autoCompleteHead();
        CodeReviewToken lastT = form.autoCompleteTail(file.getTokens().get(last.geteTokenIndex()));

        lineStart = firstT.getLine();
        lineEnd = lastT.getLine();
        indexStart = firstT.getStartIndex();
        indexEnd = lastT.getEndIndex();

        sIndex = firstT.getId();
        eIndex = lastT.getId();

        this.file.addChunk(this, last.getId() - first.getId() + 1);

        startIndexOnMatrix = startPointOnMatrix;
    }

    public int getLength() {
        return chain.getNumberOfStatement();
    }

    public FileObj getFile() {
        return file;
    }

    public int getStartIndexOnMatrix() {
        return startIndexOnMatrix;
    }

    public Chain getChain() {
        return chain;
    }

    public void setChain(Chain chain) {
        this.chain = chain;
    }

    public int getIndexStart() {
        return indexStart;
    }

    public int getIndexEnd() {
        return indexEnd;
    }

    public int getsIndex() {
        return sIndex;
    }

    public int geteIndex() {
        return eIndex;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("++++");
        if (Configuration.s_fullChunkInfo) {
            sb.append(indexStart).append(":");
            sb.append(indexEnd).append(":");
        }
        sb.append(file.getName()).append(":").append(lineStart).append("-").append(lineEnd);
        sb.append("++++\n");
        sb.append(getSource());

        return sb.toString().intern();
    }

    public String getSource() {
        StringBuffer sb = new StringBuffer();
        try {
            sb.append(file.getSource(lineStart, lineEnd));
        } catch (IOException e) {
            sb.append("Failed to read ").append(file).append(" - ").append(e.getMessage());
        }
        return sb.toString().intern();
    }

    public String getStandardizedText() {
        return FragmentFormatter.getStandardizedText(getFile().getTokens().subList(getsIndex(), geteIndex()));
    }
}
